<html>
<head><meta charset="utf-8"><title>function object ty/const mismatch · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html">function object ty/const mismatch</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="235405616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235405616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235405616">(Apr 20 2021 at 19:59)</a>:</h4>
<p><span class="user-group-mention" data-user-group-id="3805">@project-const-generics</span> </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span>::<span class="o">&lt;</span><span class="n">main</span><span class="o">&gt;</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>currently errors with</p>
<div class="codehilite"><pre><span></span><code>error[E0747]: constant provided when a type was expected
 --&gt; src/main.rs:4:11
  |
4 |     foo::&lt;main&gt;()
  |           ^^^^

error: aborting due to previous error
</code></pre></div>
<p>it's probably good to add a help message here explaining that the type of functions cannot by named directly. Anyone interested in adding that?</p>



<a name="235418541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235418541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235418541">(Apr 20 2021 at 21:19)</a>:</h4>
<blockquote>
<p>it's probably good to add a help message here explaining that the type of functions cannot by named directly</p>
</blockquote>
<p>Do you think it's likely a user is trying to do that? I think maybe it'd be better to emphasise that <code>main</code> is a specific function, not a type.</p>



<a name="235419803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235419803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235419803">(Apr 20 2021 at 21:29)</a>:</h4>
<p>i am not sure, i've seen this tried in a bug report before <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="235419920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235419920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235419920">(Apr 20 2021 at 21:30)</a>:</h4>
<p>"<code>main</code> is a specific function, not a type." that's kind of wrong though, like i guess it's the tradeoff between being 100% correct and being helpful</p>



<a name="235420016"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235420016" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235420016">(Apr 20 2021 at 21:31)</a>:</h4>
<p>I've wanted to do that before...avoiding needing extra space to store a function pointer and all.</p>



<a name="235423146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235423146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235423146">(Apr 20 2021 at 22:00)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch/near/235419920">said</a>:</p>
<blockquote>
<p>"<code>main</code> is a specific function, not a type." that's kind of wrong though, like i guess it's the tradeoff between being 100% correct and being helpful</p>
</blockquote>
<p>It's a hard balance to get right.</p>



<a name="235558536"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235558536" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> jknodt <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235558536">(Apr 21 2021 at 18:25)</a>:</h4>
<p>hm maybe just saying that the identifier for a function is a const not a type?</p>



<a name="235628456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235628456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235628456">(Apr 22 2021 at 06:59)</a>:</h4>
<p>How about "expected a type" with a note "<code>main</code> is a function".</p>



<a name="235629255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235629255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235629255">(Apr 22 2021 at 07:03)</a>:</h4>
<p>Or just keep the current error message and add a note.</p>



<a name="235629851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235629851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235629851">(Apr 22 2021 at 07:05)</a>:</h4>
<p>Is there any way to name the type of <code>main</code>?</p>



<a name="235630168"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235630168" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235630168">(Apr 22 2021 at 07:08)</a>:</h4>
<p>Well, I guess this is mentioned in <span class="user-mention silent" data-user-id="216206">lcnr</span> 's first message. How about a second note "<a href="https://doc.rust-lang.org/reference/types/function-item.html">function item types</a> cannot by named directly"</p>



<a name="235888623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235888623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235888623">(Apr 23 2021 at 18:35)</a>:</h4>
<p>that seems kinda nice, so something like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">error</span><span class="p">[</span><span class="n">E0747</span><span class="p">]</span>: <span class="nc">constant</span><span class="w"> </span><span class="n">provided</span><span class="w"> </span><span class="n">when</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">type</span> <span class="nc">was</span><span class="w"> </span><span class="n">expected</span><span class="w"></span>
<span class="w"> </span><span class="o">-</span>-&gt; <span class="nc">src</span><span class="o">/</span><span class="n">main</span><span class="p">.</span><span class="n">rs</span>:<span class="mi">4</span>:<span class="mi">11</span><span class="w"></span>
<span class="w">  </span><span class="o">|</span><span class="w"></span>
<span class="mi">4</span><span class="w"> </span><span class="o">|</span><span class="w">     </span><span class="n">foo</span>::<span class="o">&lt;</span><span class="n">main</span><span class="o">&gt;</span><span class="p">()</span><span class="w"></span>
<span class="w">  </span><span class="o">|</span><span class="w">           </span><span class="o">^^^^</span><span class="w"></span>
<span class="n">help</span>: <span class="err">`</span><span class="n">main</span><span class="err">`</span><span class="w"> </span><span class="n">is</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="n">function</span><span class="w"> </span><span class="n">item</span><span class="p">,</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="k">type</span>
<span class="nc">help</span>: <span class="nc">function</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="n">types</span><span class="w"> </span><span class="n">cannot</span><span class="w"> </span><span class="n">be</span><span class="w"> </span><span class="n">named</span><span class="w"> </span><span class="n">directly</span><span class="w"></span>
</code></pre></div>



<a name="235940465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235940465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235940465">(Apr 24 2021 at 04:26)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/astconv/generics.rs#L24-L132">https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/astconv/generics.rs#L24-L132</a></p>
<p>Is this where the error is emitted? What should I match against here? :D</p>



<a name="235949947"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235949947" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235949947">(Apr 24 2021 at 07:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="256313">Hameer Abbasi</span> <a href="#narrow/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch/near/235940465">said</a>:</p>
<blockquote>
<p><a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/astconv/generics.rs#L24-L132">https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/astconv/generics.rs#L24-L132</a></p>
<p>Is this where the error is emitted? What should I match against here? :D</p>
</blockquote>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span> this is where the message is emitted, yes</p>



<a name="235950004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950004">(Apr 24 2021 at 07:33)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/blob/a7aba58e9684175c5c5dfef8277c95ebc43f904a/compiler/rustc_typeck/src/astconv/generics.rs#L61-L113">https://github.com/rust-lang/rust/blob/a7aba58e9684175c5c5dfef8277c95ebc43f904a/compiler/rustc_typeck/src/astconv/generics.rs#L61-L113</a></p>
<p>in this match:</p>



<a name="235950006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950006">(Apr 24 2021 at 07:33)</a>:</h4>
<p>you want to check that:</p>



<a name="235950059"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950059" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950059">(Apr 24 2021 at 07:34)</a>:</h4>
<ul>
<li>the current generic param is a type parameter</li>
</ul>



<a name="235950124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950124">(Apr 24 2021 at 07:35)</a>:</h4>
<ul>
<li>the generic argument as a const arg which is just a single function item</li>
</ul>



<a name="235950171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950171">(Apr 24 2021 at 07:36)</a>:</h4>
<p>the first point is fairly straightforward, the second one is more difficult</p>



<a name="235950183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950183">(Apr 24 2021 at 07:36)</a>:</h4>
<p>soo: if the generic argument as a const arg, you have a <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.ConstArg.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/struct.ConstArg.html</a></p>



<a name="235950210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950210">(Apr 24 2021 at 07:38)</a>:</h4>
<p>you can now get the hir for the body of that const arg using <code>tcx.hir().body(value.body)</code></p>



<a name="235950375"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950375" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950375">(Apr 24 2021 at 07:41)</a>:</h4>
<p>and then match on <code>body.value.kind</code> to check if it is both a <code>ExprKind::Path</code>, with an inner <code>QPath::Resolved(_, path)</code> on which you can now match similar to what we're doing in that branch <a href="https://github.com/rust-lang/rust/blob/a7aba58e9684175c5c5dfef8277c95ebc43f904a/compiler/rustc_typeck/src/astconv/generics.rs#L62-L67">https://github.com/rust-lang/rust/blob/a7aba58e9684175c5c5dfef8277c95ebc43f904a/compiler/rustc_typeck/src/astconv/generics.rs#L62-L67</a></p>



<a name="235950430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950430">(Apr 24 2021 at 07:42)</a>:</h4>
<p>as you need to actually look inside of the <code>hir::AnonConst</code> this is a bit more cumbersome than the "expected const, found ty" case</p>



<a name="235950481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235950481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235950481">(Apr 24 2021 at 07:43)</a>:</h4>
<p>the <code>Res</code> you're searching for is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#variant.Def">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.Res.html#variant.Def</a> <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.DefKind.html#variant.Fn">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/def/enum.DefKind.html#variant.Fn</a></p>



<a name="235971316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/235971316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#235971316">(Apr 24 2021 at 13:48)</a>:</h4>
<p>PR underway, just making sure I didn't go completely bonkers. :)</p>



<a name="236046622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/function%20object%20ty/const%20mismatch/near/236046622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hameer Abbasi <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/function.20object.20ty.2Fconst.20mismatch.html#236046622">(Apr 25 2021 at 07:41)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/84520">#84520</a> is ready for review. :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>